﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TaiwuZD
{
    /// <summary>
    /// ActorBag.xaml 的交互逻辑
    /// </summary>
    public partial class ActorBag : UserControl
    {
        public ActorBag()
        {
            InitializeComponent();


            OwnedItemSource.Source = Global.Inst.CurrentRole?.OwnedItems;
            OwnedItemSource.Filter += OwnedItemSource_Filter;
            lvItemOwned.SetBinding(ListView.ItemsSourceProperty, new Binding { Source = OwnedItemSource });

            ItemTemplateSource.Source = GameAssets.TwItems.Values ;
            ItemTemplateSource.Filter += ItemTemplateSource_Filter;
            lvItemToAdd.ItemsSource = ItemTemplateSource.View;


            Global.Inst.PropertyChanged += Inst_PropertyChanged;
        }

        private void OwnedItemSource_Filter(object sender, FilterEventArgs e)
        {
            if ((TwItemType)lvItemType2.SelectedItem == TwItemType.All)
                e.Accepted = true;
            else
                e.Accepted = (e.Item as TwItemTemplate).ItemType3 == (TwItemType)lvItemType2.SelectedItem;
        }

        private void ItemTemplateSource_Filter(object sender, FilterEventArgs e)
        { 
            if ((int)lvItemType.SelectedItem == 0)
                e.Accepted = true;
            else if ((int)lvItemType.SelectedItem != (e.Item as TwItemTemplate).ItemType[0])
                e.Accepted = false;
            else if ((int)(lvItemTypeSub.SelectedItem ?? 0) == 0)
                e.Accepted = true;
            else if ((int)lvItemTypeSub.SelectedItem != (e.Item as TwItemTemplate).ItemType[1])
                e.Accepted = false;
            else if((int)(lvItemTypeSub2.SelectedItem??0) == 0)
                e.Accepted = true;
            else if ((int)lvItemTypeSub2.SelectedItem != (e.Item as TwItemTemplate).ItemType[2])
                e.Accepted = false;
            else 
                e.Accepted = true;
        }

        public CollectionViewSource OwnedItemSource { get; set; } = new CollectionViewSource();
        public CollectionViewSource ItemTemplateSource { get; set; } = new CollectionViewSource();

        public static RoutedCommand AddItemCommand { get; } = new RoutedCommand();
        public static RoutedCommand RemoveItemCommand { get; } = new RoutedCommand();

        private void AddItem_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = !string.IsNullOrWhiteSpace(Global.Inst.StorageFilePath) && lvItemToAdd?.SelectedItem != null;
        }

        private void AddItem_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var tmp = lvItemToAdd.SelectedItem as TwItemTemplate;

            //可堆叠的物品
            if (tmp[6] =="1")
            {

            }
            else
            {
                var item = Global.Inst.CreateNewItem(tmp.Id);

                if (item.ValueAt<int>(4) == 5 && item.ValueAt<int>(5) == 21)
                    item.SetBookPages(null);
                Global.Inst.CurrentRole.OwnedItems.Add(item);
            }
        }
         

        private void RemoveItem_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = !string.IsNullOrWhiteSpace(Global.Inst.StorageFilePath) && lvItemOwned?.SelectedItem != null;
        }

        private void RemoveItem_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            foreach (var item in lvItemOwned.SelectedItems.Cast<TwItem>().ToArray())
            {
                Global.Inst.CurrentRole.OwnedItems.Remove(item);
                //Global.Inst.CurrentRole.OwnedItems.Remove(lvItemOwned.SelectedItem as TwItem);
            }
            
            lvItemOwned.InvalidateProperty(ListView.ItemsSourceProperty); 
        }

        private void Inst_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(Global.CurrentRole))
            {
                OwnedItemSource.Source = Global.Inst.CurrentRole.OwnedItems;
            }
        }

        private void lvItemType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ItemTemplateSource?.View?.Refresh();
            if (lvItemTypeSub != null)
            lvItemTypeSub.SelectedIndex = 0;
        }

        private void lvItemType2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            OwnedItemSource?.View?.Refresh();
        }

        private void lvItemTypeSub_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ItemTemplateSource?.View?.Refresh();
            if (lvItemTypeSub2 != null)
                lvItemTypeSub2.SelectedIndex = 0;
        }

        private void lvItemTypeSub2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ItemTemplateSource?.View?.Refresh();
        }

        private void btnSteal_Click(object sender, RoutedEventArgs e)
        {
            Random ra = new Random();
            var idd = ra.Next(0, Global.Inst.AllRoles.Length-1);

            var target = Global.Inst.AllRoles[idd];

            while (target.OwnedItems.Count==0)
            {
                idd = ra.Next(0, Global.Inst.AllRoles.Length - 1);
                target = Global.Inst.AllRoles[idd];
            }

            idd = ra.Next(0, target.OwnedItems.Count - 1);
            var item = target.OwnedItems[idd];

            Global.Inst.CurrentRole.OwnedItems.Add(item);
            target.OwnedItems.Remove(item);

            runStealTar.Text = target.Name;
            runStealItem.Text = item.Name;
            runStealItem.Foreground = this.FindResource($"BrushL{item.Level}") as Brush;
            popSteal.IsOpen = true; 
        }
    }


    public class ItemTypeL0Converter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is int)
            {
                return GameAssets.TwItemTypes.TypesData[0][(int)value];
            }
            else return value;
        }
         

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
         
    }
    public class ItemTypeL1Converter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is int)
            {
                return GameAssets.TwItemTypes.TypesData[1][(int)value];
            }
            else return value;
        }


        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

    }
    public class ItemTypeL2Converter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is int)
            {
                return GameAssets.TwItemTypes.TypesData[2][(int)value];
            }
            else return value;
        }


        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

    }

    public class ItemSubTypesConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is int)
            {
                return GameAssets.TwItemTypes.All.Where(x => x[0] == (int)value).Select(x => x[1]).Distinct().ToArray();
            }
            else return value;
        }


        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

    }
    public class ItemSub2TypesConverter : IMultiValueConverter
    {
       
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values.Length == 2)
            {
                return GameAssets.TwItemTypes.All.Where(x => x[0] == (int)values[0]&& x[1] == (int)(values[1]??0)).Select(x => x[2]).Distinct().ToArray();
            }
            else return values;
        }
         

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class LvTypeVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is int)
            {
                return (int)value == 0 ? Visibility.Collapsed : Visibility.Visible;
            }
            else return value;
        }


        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
